# Maintainer: Martell Malone <martellmalone@gmail.com>

_realname=qemu
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.6.0
pkgrel=1
pkgdesc="A generic and open source processor emulator (mingw-w64)"
arch=('any')
license=('GPL2' 'LGPL2')
url="http://qemu.org"
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-python2"
             'perl' 'bison')
depends=("${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-gnutls"
         "${MINGW_PACKAGE_PREFIX}-gtk3"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libssh2"
         #"${MINGW_PACKAGE_PREFIX}-libusb"
         "${MINGW_PACKAGE_PREFIX}-lzo2"
         "${MINGW_PACKAGE_PREFIX}-pixman"
         "${MINGW_PACKAGE_PREFIX}-snappy"
         "${MINGW_PACKAGE_PREFIX}-SDL")
source=(http://wiki.qemu.org/download/${_realname}-${pkgver}.tar.bz2)
sha256sums=('c9ac4a651b273233d21b8bec32e30507cb9cce7900841febc330956a1a8434ec')
options=('!strip' 'debug')
_pkgfqn="${_realname}-${pkgver}"
noextract=(${_pkgfqn}.tar.bz2)

prepare() {
  [[ -d ${srcdir}/${_pkgfqn} ]] && rm -rf ${srcdir}/${_pkgfqn}

  tar -xjf ${srcdir}/${_pkgfqn}.tar.bz2 -C ${srcdir} 2>/dev/null || true
  # The QEMU tarball contains symlinks are extracted before the files they link
  # to are. But MSYS treats ln(1) like a copy, so if the symlink target doesn't
  # exist, the symlink itself can't either.
  # So extract the archive twice. The second pass gets the symlinks we couldn't
  # get in the first.
  tar -xjf ${srcdir}/${_pkgfqn}.tar.bz2 -C ${srcdir} --skip-old-files

  cd ${srcdir}/${_pkgfqn}
}

build() {
  export CC=${MINGW_PREFIX}/bin/gcc
  export CXX=${MINGW_PREFIX}/bin/g++

  mkdir -p "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"

  declare -a extra_config
  if check_option "strip" "n"; then
    extra_config+=("--disable-strip")
  fi

  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --host-cc=${CC} \
    --python=${MINGW_PREFIX}/bin/python2 \
    --datadir=${MINGW_PREFIX}/etc/qemu \
    --mandir=${MINGW_PREFIX}/share/qemu \
    --bindir=${MINGW_PREFIX}/bin \
    --libdir=${MINGW_PREFIX}/lib \
    --sysconfdir=${MINGW_PREFIX}/etc/qemu \
    --enable-libssh2 \
    --enable-lzo \
    --enable-snappy \
    --disable-docs \
    --enable-gtk \
    --with-gtkabi=3.0 \
    --disable-xen \
    --disable-vnc-sasl \
    --disable-kvm \
    --disable-libusb \
    --disable-bluez \
    --disable-spice \
    "${extra_config[@]}"

  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make -j1 DESTDIR="${pkgdir}" install
}
